package work.xkong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import work.xkong.common.ResponseRest;
import work.xkong.entity.Student;
import work.xkong.mapper.StudentMapper;
import work.xkong.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import work.xkong.vo.inputvo.StudentInfoInputVo;
import work.xkong.vo.outputvo.StudentInfoOutputVo;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author NuanYang
 * @since 2022-11-21
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public ResponseRest login(String username, String password) {
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Student::getStudentNumber, username);

        Student student = studentMapper.selectOne(lqw);

        if (student != null && student.getStudentPassword().equals(password)){
            return ResponseRest.creatSuccess("恭喜你登录成功",student);
        }

        return ResponseRest.creatFail("账号或密码错误，请重新登录");
    }

    @Override
    public ResponseRest getStudentInfo(Integer studentId) {
        StudentInfoOutputVo studentInfo = studentMapper.getStudentInfo(studentId);
        return ResponseRest.creatSuccess(studentInfo);
    }

    @Override
    public ResponseRest updateBy(Student student) {
        boolean res = studentMapper.updateById(student) > 0;

        return res ? ResponseRest.creatSuccess("更新成功") : ResponseRest.creatFail("更新失败");
    }
}
